package com.amxing.spider.listener.task;


import com.amxing.spider.listener.Listener;
import com.amxing.spider.task.*;
import com.amxing.spider.task.support.BatchTask;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dev
 * @desc
 * @createTime 2018/8/15 23:22
 */
public class BatchTaskListener<T extends BatchTask> implements Listener {

    private TaskQueue<Class<? extends Task>> queue;

    public BatchTaskListener(TaskQueue<Class<? extends Task>> queue) {
        this.queue = queue;
    }

    @Override
    public Object intercept(Object task, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        try {
            proxy.invokeSuper(task, args);
            TaskCache result = ((T) task).getTaskCache();
            /*List<Task> taskList = result.getUrl().stream().map(url -> Task.createTask(url, result.getTaskCache(), queue.clone())).filter(t ->t != null).collect(Collectors.toList());
            if (((T) task).isMultiPage() && ((T) task).hasNextPage()) {
                taskList.add(((T) task).getNextPageTask(queue));
            }*/
            //Spider.getSpider().addTask(taskList).run();
            return TaskCode.SUCCESS;
        } catch (Throwable throwable) {
            return TaskCode.EXCEPTION;
        }
    }

}
